/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author Tung
 */
public class HashMD5 {

    /**
     ** constructor
     */
    public HashMD5() {
    }
        
/**
 * 
 * @param st: string of acount password 
 * @return String: password string was encrypted
 * @throws NoSuchAlgorithmException 
 */
    public String Md5Encrypt(String st) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] md5 = new byte[32];
        md.update(st.getBytes(), 0, st.length());
        md5 = md.digest();
        return ConvertToHex(md5);
    }
    /**
     * 
     * @param data: byte[] 
     * @return String: converted string
     */

    private String ConvertToHex(byte[] data) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < data.length; i++) {
            int halfOfByte = (data[i] >>> 4) & 0x0F;
            int twoHalfBytes = 0;

            do {
                if ((0 <= halfOfByte) && (halfOfByte <= 9)) {
                    buf.append((char) ('0' + halfOfByte));
                } else {
                    buf.append((char) ('a' + (halfOfByte - 10)));
                }

                halfOfByte = data[i] & 0x0F;

            } while (twoHalfBytes++ < 1);
        }
        return buf.toString();
    }
}
